*******************************************************************
*
*	Paper:			Shining a Light on Firm Political Connections: 
*					The Role of Dark Money
*	Authors:		Matthew Denes and Madeline Marco Scanlon
*
*******************************************************************

*******************************************************************
** Set Stata settings
*******************************************************************
* Version control
version 15.1

* Set directory to replication path
cd 

* Turn off the more messaging (i.e. screen pause)
set more off


*******************************************************************
** Load main replication data
*******************************************************************
use replication_firm_year, clear


*******************************************************************
** Construct figures and table on dark money
*******************************************************************
* Count and share of firms using dark money
preserve
	* Collapse by years a firm uses dark money
	collapse (sum) dm_flag (count) firm_id, by(year)
	gen dm_flag_sum_prop = dm_flag / firm_id
	list
	
	* Count
	twoway bar dm_flag year, ///
		graphregion(color(white)) graphregion(margin()) bgcolor(white) xlabel(2008(2)2022) ///
		yscale(r(0 150)) ylabel(0(25)150) ytitle("Number of Firms")
	graph export Figures/dm_count.pdf, replace
	
	* Share
	twoway bar dm_flag_sum_prop year, ///
		graphregion(color(white)) graphregion(margin()) bgcolor(white) xlabel(2008(2)2022) ///
		yscale(r(0 .25)) ylabel(0(.05).25) ytitle("Share of Firms")
	graph export Figures/dm_share.pdf, replace
restore

* Dark money by industry
preserve
	* Construct labels
	tostring ff10, gen(ff10_string)
	gen ff10_short = "Non-durables" if ff10_string == "1"
	replace ff10_short = "Durables" if ff10_string=="2"
	replace ff10_short = "Manufacturing" if ff10_string=="3"
	replace ff10_short = "Energy" if ff10_string=="4"
	replace ff10_short = "Equipment" if ff10_string=="5"
	replace ff10_short = "Media" if ff10_string=="6"
	replace ff10_short = "Retail" if ff10_string=="7"
	replace ff10_short = "Healthcare" if ff10_string=="8"
	replace ff10_short = "Utilities" if ff10_string=="9"
	replace ff10_short = "Other" if ff10_string=="10"
	
	* Determine share of firms using dark money at any point
	* relative to firms in industry
	egen dm_ever = total(dm_flag), by(firm_id)
	replace dm_ever = dm_ever>0 & dm_ever!=.
	bysort firm_id: keep if _n==1
	collapse (sum) dm_ever (count) firm_id (first) ff10_short, by(ff10)
	
	gen dm_ind_share = dm_ever / firm_id
	gsort -dm_ind_share
	gen order = _n
	graph bar dm_ind_share, over(ff10_short, lab(angle(90)) sort(order)) ///
		graphregion(color(white)) graphregion(margin()) bgcolor(white) ///
		ytitle("Share of Industry")
	list ff10_short dm_ind_share
	graph export Figures/dm_industry.pdf, replace
restore

* Years firm use dark money by count and share
preserve
	* Collapse by years a firm uses dark money
	collapse (sum) dm_flag, by(firm_id)
	replace dm_flag = 10 if dm_flag>10 & dm_flag!=.
	drop if dm_flag==0
	collapse (count) firm_id, by(dm_flag)
	
	* Determine share
	qui: sum firm_id
	gen dm_timing = firm_id / `r(sum)'
	
	* Construct labels
	gen category = "1 year" if dm_flag==1
	replace category = "2 years" if dm_flag==2
	replace category = "3 years" if dm_flag==3
	replace category = "4 years" if dm_flag==4
	replace category = "5 years" if dm_flag==5
	replace category = "6 years" if dm_flag==6
	replace category = "7 years" if dm_flag==7
	replace category = "8 years" if dm_flag==8
	replace category = "9 years" if dm_flag==9
	replace category = "10+ years" if dm_flag>=10
	list
	
	* Count
	graph hbar firm_id, over(category, sort(dm_flag)) ///
		graphregion(color(white)) graphregion(margin()) bgcolor(white) ///
		ytitle("Number of Dark Money Firms")
	graph export Figures/dm_timing_count.pdf, replace
	
	* Share
	graph hbar dm_timing, over(category, sort(dm_flag)) ///
		graphregion(color(white)) graphregion(margin()) bgcolor(white) ///
		ytitle("Share of Dark Money Firms")
	graph export Figures/dm_timing_share.pdf, replace
restore

* Political contributions by year
preserve
	collapse (sum) dm_money pac_money lobby_money, by (year)
	label variable pac_money "PACs"
	label variable dm_money "Dark Money"
	label variable lobby_money "Lobbying"
	twoway line dm_money pac_money year, yaxis(1) ytitle("Dark Money and PACs ($ million)") || ///
		line lobby_money year, yaxis(2) ytitle("Lobbying ($ million)", axis(2)) ///
		graphregion(color(white)) graphregion(margin()) bgcolor(white) xlabel(2008(2)2022) ///
		ylabel(0(100)400, axis(1)) ylabel(0(200)800, axis(2)) legend(rows(1))
	list year dm_money pac_money lobby_money
	sum *_money
	graph export Figures/dm_pac_lobby.pdf, replace
restore

* Top 10 dark money firms
preserve 
	collapse (sum) dm_money dm_flag dm_count (first) conm, by(firm_id)
	gsort -dm_money
	list if _n<=10
restore

* Top 10 dark money groups
preserve
	use replication_dark_money, clear
	collapse (sum) dm_money, by(org_name)
	gsort -dm_money
	list if _n<=10
restore


*******************************************************************
** Summary statistics
*******************************************************************
estpost tabstat dm_flag pac_flag lobby_flag ///
	contracts_flag contracts_amount_ln subsidy_flag subsidy_money_ln, ///
	stat(n mean p50 sd) columns(stat)


*******************************************************************
** Univariate
*******************************************************************
log using Tables/univariate.txt, text replace
preserve
	* Compustat variables
	foreach cvar of varlist age mkt_lev_w tax_w roa_w tobin_q_w mkt_share {
			di ""
			di "t-test variable: `cvar'"
			if ("`cvar'"!="tobin_q_w" & "`cvar'"!="age") replace `cvar' = `cvar' * 100 // Adjust for interpretability
			ttest `cvar', by(dm_flag) unequal
			bysort dm_flag: sum `cvar', d
			kwallis `cvar', by(dm_flag)
	}
	
	* Market cap and size
	foreach cvar of varlist at_w mkt_cap_w {
			di ""
			di "t-test variable: `cvar'"
			replace `cvar' = `cvar' / 1000 // Adjust to billions (csho in millions and at in millions)
			ttest `cvar', by(dm_flag) unequal
			bysort dm_flag: sum `cvar', d
			kwallis `cvar', by(dm_flag)
	}
	
	* Other political contributions
	foreach cvar of varlist pac_money lobby_money {
			di ""
			di "t-test variable: `cvar'"
			ttest `cvar', by(dm_flag) unequal
			bysort dm_flag: sum `cvar', d
			kwallis `cvar', by(dm_flag)
	}
restore
log close


*******************************************************************
** Firm political connections
*******************************************************************
capture erase Tables/political_connections.tex
est clear
foreach y of varlist dm_flag pac_flag lobby_flag {
	est clear
	eststo `y'_nofe: reghdfe `y' dm_flag_lag pac_flag_lag lobby_flag_lag, noabsorb vce(cluster firm_id)
	eststo `y'_yearfe: reghdfe `y' dm_flag_lag pac_flag_lag lobby_flag_lag, absorb(year) vce(cluster firm_id)
	eststo `y'_yearindfe: reghdfe `y' dm_flag_lag pac_flag_lag lobby_flag_lag, absorb(year naics2) vce(cluster firm_id)
	esttab using Tables/political_connections.tex, ///
		order(dm_flag_lag pac_flag_lag lobby_flag_lag) ///
		scalars("r2_a Adjusted \(R^{2}\)" "N Observations") sfmt(%12.3f %12.0fc) ///
		se b(%9.3f) star(* 0.10 ** 0.05 *** .01) nonotes nogaps fragment noobs noconstant append
}


*******************************************************************
** Disclosure of dark money
*******************************************************************
* Outcomes: dark money, dark money for first time, PAC, lobbying
capture erase Tables/disclosure.tex
est clear
eststo board: reghdfe dm_flag L.boardskillsmatrix, absorb(year naics2) vce(cluster firm_id)
gen board_lag = L.boardskillsmatrix
estpost tabstat dm_flag board_lag if e(sample), stat(n mean p50 sd) columns(stat)
drop board_lag
eststo peers: reghdfe dm_flag L.dm_peer_ind, absorb(year naics2) vce(cluster firm_id)
gen dm_peer_lag = L.dm_peer_ind
estpost tabstat dm_flag dm_peer_lag if e(sample), stat(n mean p50 sd) columns(stat)
drop dm_peer_lag
eststo instown: reghdfe dm_flag L.inst_pct, absorb(year naics2) vce(cluster firm_id)
gen inst_pct_lag = L.inst_pct
estpost tabstat dm_flag inst_pct_lag if e(sample), stat(n mean p50 sd) columns(stat)
drop inst_pct_lag
eststo all: reghdfe dm_flag L.boardskillsmatrix L.dm_peer_ind L.inst_pct, absorb(year naics2) vce(cluster firm_id)
eststo all_pol: reghdfe dm_flag L.boardskillsmatrix L.dm_peer_ind L.inst_pct dm_flag_lag pac_flag_lag lobby_flag_lag, absorb(year naics2) vce(cluster firm_id)
esttab using Tables/disclosure.tex, ///
	scalars("r2_a Adjusted \(R^{2}\)" "N Observations") sfmt(%12.3f %12.0fc) ///
	se b(%9.3f) star(* 0.10 ** 0.05 *** .01) nonotes nogaps fragment noobs noconstant append


*******************************************************************
** Federal contracts
*******************************************************************
capture erase Tables/contracts.tex
est clear
eststo extensive: reghdfe contracts_flag dm_flag_lag, absorb(year naics2) vce(cluster firm_id)
eststo extensive_pol: reghdfe contracts_flag dm_flag_lag pac_flag_lag lobby_flag_lag, absorb(year naics2) vce(cluster firm_id)
eststo intensive: reghdfe contracts_amount_ln dm_flag_lag, absorb(year naics2) vce(cluster firm_id)
eststo intensive_pol: reghdfe contracts_amount_ln dm_flag_lag pac_flag_lag lobby_flag_lag, absorb(year naics2) vce(cluster firm_id)
esttab using Tables/contracts.tex, ///
	order(dm_flag_lag pac_flag_lag lobby_flag_lag) ///
	scalars("r2_a Adjusted \(R^{2}\)" "N Observations") sfmt(%12.3f %12.0fc) ///
	se b(%9.3f) star(* 0.10 ** 0.05 *** .01)  nonotes nogaps fragment noobs noconstant replace


*******************************************************************
** Government subsidies
*******************************************************************
capture erase Tables/subsidies.tex
est clear
eststo extensive: reghdfe subsidy_flag dm_flag_lag, absorb(year naics2) vce(cluster firm_id)
eststo extensive_pol: reghdfe subsidy_flag dm_flag_lag pac_flag_lag lobby_flag_lag, absorb(year naics2) vce(cluster firm_id)
eststo intensive: reghdfe subsidy_money_ln dm_flag_lag, absorb(year naics2) vce(cluster firm_id)
eststo intensive_pol: reghdfe subsidy_money_ln dm_flag_lag pac_flag_lag lobby_flag_lag, absorb(year naics2) vce(cluster firm_id)
esttab using Tables/subsidies.tex, ///
	order(dm_flag_lag pac_flag_lag lobby_flag_lag) ///
	scalars("r2_a Adjusted \(R^{2}\)" "N Observations") sfmt(%12.3f %12.0fc) ///
	se b(%9.3f) star(* 0.10 ** 0.05 *** .01) nonotes nogaps fragment noobs noconstant replace


*******************************************************************
** Industry allocation
*******************************************************************
preserve
	* Collapse by count and amount for contracts and subsidies
	* Use amount of political contributions
	collapse (sum) contracts_flag subsidy_flag ///
		(sum) contracts_amount subsidy_money ///
		dm_flag dm_money pac_money lobby_money, by(naics4 year)
	xtset naics4 year
	
	* Log of amounts
	gen dm_money_ln = ln(1 + dm_money)
	gen pac_money_ln = ln(1 + pac_money)
	gen lobby_money_ln = ln(1 + lobby_money)
	gen contracts_amount_ln = ln(1 + contracts_amount)
	gen subsidy_money_ln = ln(1 + subsidy_money)
	
	* Regressions
	capture erase Tables/industry_benefits.tex
	est clear
	eststo contracts_ind: ppmlhdfe contracts_flag L.dm_money_ln L.pac_money_ln L.lobby_money_ln, absorb(year naics4) vce(cluster naics4)
	eststo contracts_amt: reghdfe contracts_amount_ln L.dm_money_ln L.pac_money_ln L.lobby_money_ln, absorb(year naics4) vce(cluster naics4)
	eststo subsidies_ind: ppmlhdfe subsidy_flag L.dm_money_ln L.pac_money_ln L.lobby_money_ln, absorb(year naics4) vce(cluster naics4)
	eststo subsidies_amt: reghdfe subsidy_money_ln L.dm_money_ln L.pac_money_ln L.lobby_money_ln, absorb(year naics4) vce(cluster naics4)
	esttab using Tables/industry_benefits.tex, ///
		scalars("r2_a Adjusted \(R^{2}\)" "N Observations") sfmt(%12.3f %12.0fc) ///
		se b(%9.3f) star(* 0.10 ** 0.05 *** .01) nonotes nogaps fragment noobs noconstant replace
	
	* Summary statistics
	estpost tabstat dm_money_ln pac_money_ln lobby_money_ln ///
		contracts_flag contracts_amount_ln subsidy_flag subsidy_money_ln if e(sample), ///
		stat(n mean p50 sd) columns(stat)
restore

